using System.Collections.Generic;
using System.Data;

namespace Glpt.Data
{
    /// <summary>
    /// 用户信息类
    /// </summary>
    public class UserInfo
    {
        /// <summary>
        /// 用户名
        /// </summary>
        public string UserName = "";
        /// <summary>
        /// 密码
        /// </summary>
        public string PassWord = "";
        /// <summary>
        /// 真实姓名
        /// </summary>
        public string TrueName = "";
        /// <summary>
        /// 用户组
        /// </summary>
        public string USERGROUP = "";
        /// <summary>
        /// EMAIL地址
        /// </summary>
        public string EMAIL = "";
        /// <summary>
        /// 手机号码
        /// </summary>
        public string MOBILE = "";
        /// <summary>
        /// IP限制
        /// </summary>
        public string IP_LIMIT = "";
        /// <summary>
        /// MAC限制
        /// </summary>
        public string MAC_LIMIT = "";
        /// <summary>
        /// 是否显示在线
        /// </summary>
        public bool IS_ONLINE = false;
        /// <summary>
        /// 是否是管理员
        /// </summary>
        public bool IsAdmin = false;

        /// <summary>
        /// 初始化类的新实例
        /// </summary>
        /// <param name="dr">数据行</param>
        public UserInfo(DataRow dr)
        {
            if (dr != null)
            {
                UserName = dr["USERNAME"].ToString();
                PassWord = dr["PASSWORD"].ToString();
                USERGROUP = dr["USERGROUP"].ToString();
                TrueName = dr["TRUENAME"].ToString();
                EMAIL = dr["EMAIL"].ToString();
                MOBILE = dr["MOBILE"].ToString();
                IP_LIMIT = dr["IP_LIMIT"].ToString().Trim();
                MAC_LIMIT = dr["MAC_LIMIT"].ToString().Trim();
                IsAdmin = dr["IS_LEADER"].ToString().Equals("2");
                IS_ONLINE = dr["IS_ONLINE"].ToString().Equals("1");
            }
        }

        /// <summary>
        /// 获取替换后的用户名
        /// </summary>
        /// <param name="user_name">原用户名</param>
        /// <returns>替换后的用户名</returns>
        public string GetReplaceName(string user_name)
        {
            string val = user_name.Replace("'@USERNAME'", "'" + UserName + "'").Replace("@USERNAME", "'" + UserName + "'");
            val = val.Replace("'@TRUENAME'", "'" + TrueName + "'").Replace("@TRUENAME", "'" + TrueName + "'");
            val = val.Replace("'@USERGROUP'", "'" + USERGROUP + "'").Replace("@USERGROUP", "'" + USERGROUP + "'");
            return val;
        }
    }

    /// <summary>
    /// 数据库项目集合
    /// </summary>
    public class UserInfoCollection : List<UserInfo>
    {
        /// <summary>
        /// 按用户名获取用户信息
        /// </summary>
        /// <param name="username">用户名</param>
        /// <returns>用户信息</returns>
        public UserInfo this[string username]
        {
            get
            {
                UserInfo item = null;
                for (int i = 0; i < base.Count; i++)
                {
                    if (base[i].UserName.Equals(username))
                    {
                        item = base[i];
                        break;
                    }
                }
                return item;
            }
        }

        /// <summary>
        /// 转换成表
        /// </summary>
        /// <returns>列表</returns>
        public DataTable ToTable()
        {
            DataTable dt = new DataTable();
            dt.CaseSensitive = true;
            dt.Columns.Add("USERNAME");
            dt.Columns.Add("TRUENAME");
            for (int i = 0; i < base.Count; i++)
            {
                DataRow dr = dt.NewRow();
                dr["USERNAME"] = base[i].UserName;
                dr["TRUENAME"] = base[i].TrueName;
                dt.Rows.Add(dr);
            }
            return dt;
        }
    }
}
